<template>
  <div class="set">
    <div class="set_ct">
      <Form ref="formValidate"
            :model="formData"
            :rules="ruleValidate"
            :label-width="100"
            class="set_ct_form">
        <FormItem label="补卡收费金额:"
                  prop="price">
          <Input v-model="formData.price"
                 :maxlength="5"
                 @on-keyup="changeprice"
                 style="width:280px;"
                 placeholder="可输入范围为0到99之间"
                 @on-blur="changeblur"></Input>

          <span style="padding-left:8px;">元</span>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  class="btn"
                  @click="handleSubmit">确认</Button>
          <Button class="btn"
                  style="margin-left:20px;"
                  @click="cancel">取消</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>
<script>
import { getDicQuery, setDicSave } from '@/api/setting'
import { routeEqual } from '@/libs/util'
import './index.less'
export default {
  data () {
    return {
      title: '补卡收费金额设置',
      formData: {
        price: '10'
      },
      ruleValidate: {
        price: [{ required: true, type: 'string', message: '补卡收费金额不能为空', trigger: 'blur' }, { required: true, message: '可输入范围为1到99之间', trigger: 'blur', pattern: /^\d{1,2}(\.\d{1,2})?$/ }]
      }
    }
  },
  methods: {
    changeprice () {
      // this.formData.price = this.formData.price.replace(/^(0+)|[^\d]+/g, '')
      if (this.formData.price.length === 1) {
        this.formData.price = this.formData.price.replace(/[^0-9]/g, '')
      } else {
        this.formData.price = this.formData.price.replace(/^(0+)|[^\d]+/g, '')
      }
      if (this.formData.price > 99) {
        this.formData.price = this.formData.price.substring(0, 2)
      }
    },
    submitForm () {
      getDicQuery({
        'dictKeys': 'cardReplacementChargeAmount',
        'merchantId': this.$store.state.user.merchantId,
        'msType': '0'
      }).then((res) => {
        if (res.data.status === 200) {
          if (res.data.data[0].cardReplacementChargeAmount) {
            this.formData.price = res.data.data[0].cardReplacementChargeAmount
          } else {
            this.formData.price = '0.00'
          }
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    cancel () {
      // this.formData.price = null
      this.closeModal()
    },
    changeblur () {
      this.formData.price = this.formData.price + ''
      this.formData.price = this.formData.price.replace(/[^\d.]/g, '')
      if (this.formData.price === '') {
        this.formData.price = ''
      } else if (this.formData.price.indexOf('.') === -1) {
        this.formData.price = this.formData.price + '.00'
      }
    },
    // 关闭弹窗
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    },
    handleSubmit () {
      this.$refs.formValidate.validate((val) => {
        if (val) {
          setDicSave({
            'cardReplacementChargeAmount': this.formData.price,
            'merchantId': this.$store.state.user.merchantId,
            'msType': '0'
          }).then(res => {
            if (res.data.status === 200) {
              this.$Message.success('修改成功')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    }
  },
  mounted () {
    this.submitForm()
    if (!this.hasPermission('moms:globalsetting:reissuecardfee')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
